package com.njlime.cms.repository.app;

import com.njlime.cms.entities.CommunityComment;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Repository
public class AppCommunityCommentDaoImpl extends BaseDaoImpl<CommunityComment> {

    /**
     * 保存
     * @param communityComment
     * @return
     */
    public int saveCommunity(CommunityComment communityComment){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("insert into t_community_comment ");
        sb.append("(community_id,comment,user_id,user_name,create_time) ");
        sb.append("values(?,?,?,?,?  )");
        arrList.add(communityComment.getCommunity_id());
        arrList.add(communityComment.getComment());
        arrList.add(communityComment.getUser_id());
        arrList.add(communityComment.getUser_name());
        arrList.add(communityComment.getCreate_time());
        return  updateJdbc(sb.toString(), arrList);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public Map<String, Object> get(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_community where id  = ?");
        arrList.add(id);
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 评论列表
     * @param pageBean
     * @param info
     * @return
     */
    public PageBean list(PageBean pageBean, CommunityComment info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select rc.*,m.head_url from t_community_comment rc, t_member m");
        sb.append(" where rc.is_del != 1");
        sb.append(" and rc.community_id = ?");
        sb.append(" and rc.user_id = m.id");
        sb.append(" and rc.status = 1");
        arrList.add(info.getCommunity_id());
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by rc.is_top desc, rc.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 奖品列表
     * @param informationID
     * @return
     */
    public PageBean rewardList(PageBean pageBean, Integer informationID){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select cc.id,cc.is_reward,cc.user_name,cc.create_time,v.head_url from t_community_comment cc , t_member v");
        sb.append(" where cc.is_del != 1");
        sb.append(" and cc.community_id = ?");
        sb.append(" and cc.user_id = v.id ");
        sb.append(" and cc.is_reward = 1");
        arrList.add(informationID);
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by cc.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 点赞数
     * @param id
     */
    public void fabulousNumUpdate(Integer id, int num){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_community_comment SET fabulous_num = fabulous_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }
}
